 #include<reg51.h>
 #include<stdio.h>
#include "lcd.h"

sbit RF_A=P0^0;
sbit RF_B=P0^1;
sbit RF_C=P0^2;
sbit RF_D=P0^3;
sbit m1=P1^4;
sbit m2=P1^5;
sbit m3=P1^6;
sbit m4=P1^7;
sbit ir=P1^1;
//unsigned int data_out,command=0x80,temp;
//sfr lcd_data_pin=0xA0; //P2 port

//char display[]={"hi"};


sbit fan=P1^0;
unsigned int i,j,p;  
char cout = 0x80; 
sbit DQ  =  P3^6;	// connect with DS1820 Data pin
unsigned char MyTemp[9];

void wait_state(unsigned int y);
void right_scroll(unsigned char *name,unsigned int delay2);
void init_lcd();
void lcd_en ();      

void display();     
//----------------------------------------------------------------------------------------//
                              //TEMPRATURE SENSOR DS1820 ROTUNE BEGIN//   
//----------------------------------------------------------------------------------------//
//---------------------------------------
// Delay mS function
//---------------------------------------
void DelayMs(unsigned int count) 
{  // mSec Delay 11.0592 Mhz 
    unsigned int i;		       		// RIDE v6.1.6 
    while(count) {
        i = 115; 
		while(i>0) i--;
        count--;
    }
}
//----------------------------------------
// DELAY at 11.0592MHz crystal.
// Calling the routine takes about 22us, and then
// each count takes another 17us.
// //----------------------------------------
void DelayUs(int us)
{
	int i;
	for (i=0; i<us; i++);
}
//----------------------------------------
// Reset DS1820
//----------------------------------------
bit ResetDS1820(void)
{
	bit presence;
	DQ = 0; 		//pull DQ line low
	DelayUs(29); 	// leave it low for about 490us
	DQ = 1; 		// allow line to return high
	DelayUs(3); 	// wait for presence 55 uS
	presence = DQ; 	// get presence signal
	DelayUs(25); 	// wait for end of timeslot 316 uS 
	return(presence); // presence signal returned
} 	// 0=presence, 1 = no part
//-----------------------------------------
// Read one bit from DS1820
//-----------------------------------------
bit ReadBit(void)
{
	unsigned char i=0;
	DQ = 0; 	// pull DQ low to start timeslot
	DQ=1;
	for (i=0; i<3; i++); // delay 17 us from start of timeslot
	return(DQ); // return value of DQ line
}
//-----------------------------------------
// Write one bit to DS1820
//-----------------------------------------
void WriteBit(bit Dbit)
{

   DQ=0;	
	DQ = Dbit ? 1:0;
	DelayUs(5); 			// delay about 39 uS
	DQ = 1;
}
//-----------------------------------------
// Read 1 byte from DS1820
//-----------------------------------------
unsigned char ReadByte(void)
{
	unsigned char i;
	unsigned char Din = 0;
	for (i=0;i<8;i++)
	{
		Din|=ReadBit()? 0x01<<i:Din;
		DelayUs(6); 
	}
	return(Din);
}
//-----------------------------------------
// Write 1 byte
//-----------------------------------------
void WriteByte(unsigned char Dout)
{
	unsigned char i;
	for (i=0; i<8; i++) // writes byte, one bit at a time
	{	    
		WriteBit((bit)(Dout & 0x1)); 		// write bit in temp into
		Dout = Dout >> 1;
	}
	DelayUs(5);
}
//-----------------------------------------
// Read temperature
//-----------------------------------------
void ReadTemp(unsigned char * buff)
{
	unsigned char n;
	
	EA=0;	// disable all interrupt
	ResetDS1820();
    WriteByte(0xcc);  // skip ROM
    WriteByte(0x44);  // perform temperature conversion
    while (ReadByte()==0xff); // wait for conversion complete	
    ResetDS1820();
    WriteByte(0xcc);  // skip ROM
    WriteByte(0xbe);  // read the result
    
    for (n=0; n<9; n++)     // read 9 bytes but, use only one byte
    {
       buff[n]=ReadByte();  // read DS1820
    }
	EA=1;
}
//-----------------------------------------------------------------------------------------//
                            //2*16 LCD ROUTINE START HERE
//-----------------------------------------------------------------------------------------//
void delay()
  {
	  unsigned int i =100000;
	  while(i--);
  }
void delay1()
  {
	  unsigned int i =100;
	  while(i--);
  }

void disp(char name1)
    	 {
 	        rs=0x01;
  			  P2=name1;
  			  lcd_en ();
 			  delay();
 	   	}
  void display(char *name)
 	 {
 		int l = strlen(name);
 		int i;
 		for(i = 0;i < l;i++)
   		{
 		    rs = 0x01;
 		 	 P2 = name[i];
 		 	 lcd_en ();
 			 delay1();
   	 	}   
	  	}

void lcd_comm()
{
	 	rs   = 0x00;
}
 void init_lcd()
        { 
          unsigned char x[] = {0x30,0x0c,0x01,0x06,0x80}; 
          unsigned char i;
          for(i=0;i<=4;i++)
             {
                lcd_comm();
                P2       =x[i];
           	    lcd_en ();
           	    delay1();
           	 }
         } 
               
  void display1(unsigned char *name,unsigned int delay)
 	           {
                  int l  = strlen(name);
            		int i=0;
            		for( i =0;i<l;i++)
 		             {
 		                rs = 0x01;
 		      			 P2 = name[i];
 		      			 lcd_en ();
 		      		    wait_state(delay);
 		      		   }
 		      		}	    
		
//----------------------------------------------------------------------------------------------------//
                               //LCD ROUTINE END HERE//
//---------------------------------------------------------------------------------------------------//
 void wait_state(unsigned int q)
            {
              unsigned int i,j;
              for(i=0;i<q;i++)
               for(j=0;j<498;j++);
            }
          
//----------------------------------------------------------------------------------------//
                             //END OF TEMPERATURE SENSOR DS1820 ROUTINE// 
//----------------------------------------------------------------------------------------//		

//---------------------------------------------------------------------------------------------------------------------//

 

void main()
 {
      char ch,tp,tpd;
   init_lcd();
//   display1("TEMP:   ",0);
fan=0;
P0=0x3f;
while(1)
{
		fan=0;
		wait_state(100);
      ReadTemp(&MyTemp[0]);        
		tp  = MyTemp[0] >> 1;
//	tpd = ((MyTemp[0] >> 1)&1) ? 5:0;
              
             lcd_comm();  
             P2=0x0C;
             lcd_en ();
             
             lcd_comm();  
             P2=0x80;
             lcd_en ();
             delay();
             disp(((tp%100)/10)+48);
             disp((tp%10)+48);
            // display(".");
            // disp((tpd)+48);
             delay1();
		wait_state(100);
 if(RF_A==0 && RF_B==0)
 {
 	m1=0;
 	m2=0;
 	m3=0;
 	m4=0;
 }
else if(RF_A==0)//f
	 {
 	m1=1;
 	m2=0;
 	m3=0;
 	m4=1;
	}
 else if(RF_B==0)//b
 	{
 	m1=0;
 	m2=1;
 	m3=1;
 	m4=0;
 }     
 else if(RF_C==0)//r
 {
 		m1=0;
 	m2=0;
 	m3=0;
 	m4=1;
 }     
else if(RF_D==0)//l
 {
 
 	m1=1;
 	m2=0;
 	m3=0;
 	m4=0;
 }

 if(ir==1)
{
 		m1=0;
 	m2=0;
 	m3=0;
 	m4=1;
 }
    
    if(tp>40)
    {
    fan=1;
    DelayMs(3000);
    }
    else  if(tp>50)
    {
    fan=1;
    DelayMs(4000);
     }
     else  if(tp>60)
    {
    fan=1;
    DelayMs(5000);
    }   
    fan=0;
   P0=0x3f;   	
}

}
